'use strict';

const { Controller } = require('ee-core');
const { app } = require('electron')
const mqtt = require('mqtt')


const winURL = !app.isPackaged
    ? `http://localhost:8080` // 开发路径
    : `file://${__dirname}/index.html` //生产路径

/**
 * example
 * @class
 */
class MqttController extends Controller {

  constructor(ctx) {
    super(ctx);
  }

  async startMQTT() {
    console.log('开始连接MQTT')
    const client = mqtt.connect('mqtts://l76a0de8.ala.cn-hangzhou.emqxsl.cn:8883', {
      clientId: 'electron-app-client',
      username: 'kcyu1',
      password: '4404050'
    })
    const topic = '/sfhErp/newOrder'
    client.subscribe(topic, { qos: 1 }, (error) => {
      if (error) {
        console.log('subscribe error:', error)
        return
      }
      console.log(`Subscribe to topic '${topic}'`)
    })

    client.on('message', (topic, payload) => {
      console.log('Received Message:', topic, payload.toString())
    })
  }


}

MqttController.toString = () => '[class MqttController]';
module.exports = MqttController;